// import DB from 'model/DB';
import { Manifest } from 'm3u8-parser';
import { IDownloadDBTask } from 'model/DB';
import { MemoryRouter as Router, Routes, Route } from 'react-router-dom';
import { IUser } from './contexts/GlobalContext';
import GlobalProvider from './views/base/GlobalProvider';
import RequireAuth from './views/base/RequireAuth';
import Download from './views/download/Download';
import Home from './views/home/Home';
import Login from './views/login/Login';
import Setting from './views/setting/Setting';

declare global {
  interface Window {
    electron: {
      ipcRenderer: {
        myPing: () => void
        on: (channel: string, func: any) => void
        once: (channel: string, func: any) => void
        exit: () => void
        login: () => void
        getStoreValue: (key: string) => Promise<any>
        getLocalConfig: () => Promise<any>
        setStoreValue: (key:string, data: any) => void
        startDownloadM3U8: (data: {
          // 下载 m3u8 地址
          url: string
          // 下载 m3u8 本地保存地址
          filePath: string
          // m3u8 文件名
          fileName: string
        }) => Manifest
        startDownloadTS: (data: {
          // 下载 ts 地址
          url: string
          // 下载 ts 本地保存地址
          filePath: string,
          taskId: number,
          name: string
        }) => void
        setSavePath: (path?: string) => any
        deleteM3U8: (filePath: string) => void

        // 获取本地目录
        getDirectory: () => string
    
        // 导入本地课程
        importLocalCourse: (path: string) => IDownloadDBTask[]

        // 写入本地课程 json 文件
        writeTask: (task: IDownloadDBTask) => void

        setStaticPath: (staticPath: string) => void
      }
      log: any,
      crypto: {
        md5: (info: string) => string
      },
      path: any
    }

  }
}

interface AppProps {
  user: IUser
}

export default function App(props: AppProps) {

  console.log('app render', props)

  return (
    <Router>
      <GlobalProvider user={props.user}>
        <Routes>
          <Route path="/" element={
            <RequireAuth><Home /></RequireAuth>
          }/>
          <Route path="/home" element={
            <RequireAuth><Home /></RequireAuth>
          }/>
          {/* <Route path="/course" element={
            <RequireAuth><Course /></RequireAuth>
          }/> */}

          <Route path="/download" element={
            <RequireAuth><Download /></RequireAuth>
          }/>
          <Route path="/setting" element={
            <RequireAuth><Setting /></RequireAuth>
          }/>
          <Route path="/login" element={
            <Login />
          }/>

        </Routes>
      </GlobalProvider>

    </Router>
  );
}
